<template>
	<view class="container">
		<view class="info">
			<view class="buttons">
				<span class="buttons1" @click="handleCancel">取消</span>
				<span class="buttons2" @click="handleConfirm">确定</span>
			</view>
			<view class="avatars">
				<view class="avatar-grid">
					<!-- 现有头像列表 -->
					<view v-for="avatar in avatarList" :key="avatar.id" class="avatar-item"
						:class="{ selected: selectedAvatarId === avatar.id }" @click="selectAvatar(avatar.id)">
						<image :src="avatar.url" class="avatar-image" mode="aspectFill"></image>
						<!-- 选择框 -->
						<view class="select-box" :class="{ active: selectedAvatarId === avatar.id }">
							<view class="select-icon" v-if="selectedAvatarId === avatar.id"><span>✓</span></view>
						</view>
						<!-- 删除按钮 -->
						<view class="delete-btn" @click.stop="deleteAvatar(avatar.id)"><span>x</span></view>
					</view>

					<!-- 上传头像占位符 (只在头像数量少于9个时显示) -->
					<view v-if="avatarList.length < 9" class="avatar-item upload-placeholder" @click="uploadAvatar">
						<view class="upload-icon">+</view>
						<text class="upload-text">上传头像</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedAvatarId: null, // 当前选中的头像ID
				avatarList: [{
						id: 1,
						url: '/static/tuangou-youhui/优惠卷.png',
						userId: 1
					},
					{
						id: 2,
						url: '/static/tuangou-youhui/优惠卷.png',
						userId: 1
					},
					{
						id: 3,
						url: '/static/tuangou-youhui/优惠卷.png',
						userId: 1
					},
					{
						id: 4,
						url: '/static/tuangou-youhui/优惠卷.png',
						userId: 1
					},
					{
						id: 5,
						url: '/static/tuangou-youhui/优惠卷.png',
						userId: 1
					}
				]
			}
		},
		methods: {
			handleCancel() {
				uni.navigateBack();
			},
			handleConfirm() {
				if (!this.selectedAvatarId) {
					uni.showToast({
						title: '请选择头像',
						icon: 'none',
						duration: 1500
					});
					return;
				}

				// 这里可以调用API更新用户头像
				console.log('选中的头像ID:', this.selectedAvatarId);

				uni.showToast({
					title: '头像更新成功',
					icon: 'success',
					duration: 1500
				});

				// 延迟返回上一页
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			},
			// 选择头像
			selectAvatar(avatarId) {
				this.selectedAvatarId = avatarId;
			},
			// 删除头像
			deleteAvatar(avatarId) {
				uni.showModal({
					title: '确认删除',
					content: '确定要删除这个头像吗？',
					success: (res) => {
						if (res.confirm) {
							// 从列表中移除
							this.avatarList = this.avatarList.filter(avatar => avatar.id !== avatarId);
							// 如果删除的是当前选中的头像，清除选中状态
							if (this.selectedAvatarId === avatarId) {
								this.selectedAvatarId = null;
							}
							uni.showToast({
								title: '删除成功',
								icon: 'success',
								duration: 1000
							});
						}
					}
				});
			},
			// 上传头像
			uploadAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const tempFilePath = res.tempFilePaths[0];
						// 这里可以上传到服务器，获取URL后添加到列表
						// 模拟添加新头像
						const newAvatar = {
							id: Date.now(), // 临时使用时间戳作为ID
							url: tempFilePath,
							userId: 1
						};
						this.avatarList.push(newAvatar);

						uni.showToast({
							title: '上传成功',
							icon: 'success',
							duration: 1000
						});
					},
					fail: (err) => {
						console.log('选择图片失败:', err);
					}
				});
			}
		}
	}
</script>

<style>
	.container {
		background: linear-gradient(to bottom, #3284fc 50%, #f5f5f5 50%);
		min-height: 40vh;
		max-height: 40vh;
		padding: 10rpx 20rpx;
		box-sizing: border-box;
	}

	.info {
		margin-top: 30rpx;
		background: #fff;
		border-radius: 30rpx;
		padding: 30rpx;
		min-height: 700rpx;
		box-sizing: border-box;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
	}

	.buttons {
		display: flex;
		justify-content: space-between;
		padding: 10rpx 20rpx;
		border-bottom: 8rpx solid #f0f2f5;
		font-size: 32rpx;
		font-weight: 600;
		margin-bottom: 40rpx;
	}

	.buttons1 {
		color: #000000;
	}

	.buttons2 {
		color: #3284fc;
	}

	.avatars {
		box-sizing: border-box;
		padding: 20rpx 0;
	}

	.avatar-grid {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		gap: 34rpx;
	}

	.avatar-item {
		position: relative;
		width: 180rpx;
		height: 180rpx;
		border-radius: 20rpx;
		overflow: hidden;
		background: #f8f9fa;
		border: 2rpx solid #e9ecef;
		transition: all 0.3s ease;
	}

	.avatar-item:active {
		transform: scale(0.95);
	}

	.avatar-image {
		width: 100%;
		height: 100%;
		border-radius: 20rpx;
	}

	.upload-placeholder {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		border: 2rpx dashed #ced4da;
		background: #f8f9fa;
		cursor: pointer;
	}

	.upload-placeholder:hover {
		border-color: #3284fc;
		background: #f0f7ff;
	}

	.upload-icon {
		font-size: 60rpx;
		color: #6c757d;
		margin-bottom: 10rpx;
	}

	.upload-text {
		font-size: 24rpx;
		color: #6c757d;
	}

	.select-box {
		position: absolute;
		top: 8rpx;
		left: 8rpx;
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		border: 3rpx solid #fff;
		background: rgba(255, 255, 255, 0.8);
		display: flex;
		align-items: center;
		justify-content: center;
		transition: all 0.3s ease;
	}

	.select-box.active {
		background: #3284fc;
		border-color: #3284fc;
	}

	.select-icon {
		color: #fff;
		font-size: 24rpx;
		font-weight: bold;
	}

	.delete-btn {
		position: absolute;
		top: 8rpx;
		right: 8rpx;
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		background: rgba(255, 59, 48, 0.9);
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		font-size: 30rpx;
		font-weight: bold;
	}

	.delete-btn span {
		padding-bottom: 4.5rpx;
	}
	/* 选中状态的头像边框 */
	.avatar-item.selected {
		border-color: #3284fc;
		box-shadow: 0 0 0 4rpx rgba(50, 132, 252, 0.2);
	}
</style>